package com.android.aircondition.common;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.aircondition.R;
import com.android.aircondition.service.GreeService;
import com.android.aircondition.service.Setting;

public class NetListAdapter extends BaseAdapter {

	private Activity activity;
	private Setting  setting;
	private GreeService service;
	
	public NetListAdapter(Activity activity) {
		this.activity = activity;
		this.service  = new GreeService(activity);
		setting = this.service.getSettingPreference();
	}
	
	public int getCount() {

		return 1;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		View view = LayoutInflater.from(activity).inflate(R.layout.net_list_item, null);
		
		final LinearLayout switchLayout  = (LinearLayout) view.findViewById(R.id.switchLinear);
		
		TextView switchOnTextView  = (TextView) view.findViewById(R.id.switchOnTextView);
		
		TextView switchOffTextView = (TextView) view.findViewById(R.id.switchOffTextView);
		
		TextView itemTitleTextView = (TextView) view.findViewById(R.id.netItemTitle);
		
		itemTitleTextView.setText(setting.getTitle());
		
		int res = setting.isSwitchOn()?R.drawable.swith_on:R.drawable.swith_off;
		
		switchLayout.setBackgroundResource(res);
		
		OnClickListener switchListener = new OnClickListener() {
			
			public void onClick(View v) {
				
				switch (v.getId()) {
				
				case R.id.switchOnTextView:
					switchLayout.setBackgroundResource(R.drawable.swith_on);
					setting.setSwitchOn(true);
					break;
					
				case R.id.switchOffTextView:
					switchLayout.setBackgroundResource(R.drawable.swith_off);
					setting.setSwitchOn(false);
					break;
				}
				
				service.setSettingPreference(setting);
				
			}
		};
		
		switchOffTextView.setOnClickListener(switchListener);
		switchOnTextView .setOnClickListener(switchListener);
		
		return view;
	}	
	

}
